/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package com.lineage.server.model.item.etcitem.action;

import com.lineage.server.codes.ActionCodes;
import com.lineage.server.controller.FishingTimeController;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.item.ItemExecutor;
import com.lineage.server.packetserver.S_Fishing;
import com.lineage.server.packetserver.S_ServerMessage;
import com.lineage.server.utils.Random;

/**
 * Class <code>FishingRod</code> 魔法钓竿 - 41293.
 * 
 * @author jrwz
 * @version 2012-6-19下午04:43:17
 * @see com.lineage.server.model.item.etcitem.action
 * @since JDK1.7
 */
public final class FishingRod extends ItemExecutor {

    /**
     * 取得实例.
     * 
     * @return 实例
     */
    public static ItemExecutor get() {
        return new FishingRod();
    }

    /** 魔法钓竿 - 41293. */
    private FishingRod() {
    }

    @Override
    public void execute(final L1PcInstance pc, final L1ItemInstance useItem,
            final int readC1, final int readC2, final int targetId,
            final int targetX, final int targetY) {
        final int itemId = useItem.getItemId();
        final int fishX = targetX;
        final int fishY = targetY;
        this.startFishing(pc, itemId, fishX, fishY);
    }

    // 钓鱼
    private void startFishing(final L1PcInstance pc, final int itemId,
            final int fishX, final int fishY) {
        if ((pc.getMapId() != 5300) && (pc.getMapId() != 5301)) {
            pc.sendPackets(new S_ServerMessage(1138)); // 无法在这个地区使用钓竿。
            return;
        }
        if (pc.getTempCharGfx() != pc.getClassId()) {
            pc.sendPackets(new S_ServerMessage(1170)); // 这里不可以变身。
            return;
        }

        final int rodLength = 6;

        if (pc.getMap().isFishingZone(fishX, fishY)) {
            if (pc.getMap().isFishingZone(fishX + 1, fishY)
                    && pc.getMap().isFishingZone(fishX - 1, fishY)
                    && pc.getMap().isFishingZone(fishX, fishY + 1)
                    && pc.getMap().isFishingZone(fishX, fishY - 1)) {
                if ((fishX > pc.getX() + rodLength)
                        || (fishX < pc.getX() - rodLength)) {
                    pc.sendPackets(new S_ServerMessage(1138));
                } else if ((fishY > pc.getY() + rodLength)
                        || (fishY < pc.getY() - rodLength)) {
                    pc.sendPackets(new S_ServerMessage(1138));
                } else if (pc.getInventory().consumeItem(47103, 1)) { // 新鲜的饵
                    pc.setFishX(fishX);
                    pc.setFishY(fishY);
                    pc.sendPackets(new S_Fishing(pc.getId(),
                            ActionCodes.ACTION_FISHING, fishX, fishY));
                    pc.broadcastPacket(new S_Fishing(pc.getId(),
                            ActionCodes.ACTION_FISHING, fishX, fishY));
                    pc.setFishing(true);
                    final long time = System.currentTimeMillis() + 10000
                            + Random.nextInt(5) * 1000;
                    pc.setFishingTime(time);
                    FishingTimeController.getInstance().addMember(pc);
                } else {
                    pc.sendPackets(new S_ServerMessage(1137)); // 钓鱼需要有饵。
                }
            } else {
                pc.sendPackets(new S_ServerMessage(1138)); // 无法在这个地区使用钓竿。
            }
        } else {
            pc.sendPackets(new S_ServerMessage(1138)); // 无法在这个地区使用钓竿。
        }
    }
}
